#include "changepasswordwindow.h"
#include "ui_changepasswordwindow.h"
#include <QKeyEvent>
#include <QtSql/QSqlQueryModel>
#include <QtSql/QSqlQuery>
#include <QMessageBox>
#include <QDebug>
changePasswordWindow::changePasswordWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::changePasswordWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("修改密码");

    //将字母区回车键与确认绑定在一起
    ui->pushButton->setShortcut(Qt::Key_Return);

    //设置背景图
    this->setAutoFillBackground(true); // 这句要加上, 否则可能显示不出背景图.
    QPalette palette = this->palette();
    palette.setBrush(QPalette::Window,
                     QBrush(QPixmap(":/imageUsed/login.png").scaled( this->size(),
                                                     Qt::IgnoreAspectRatio,
                                                     Qt::SmoothTransformation))); // 使用平滑的缩放方式
    this->setPalette(palette); // 已给widget加上了背景图.

    //按钮样式表
    this->setStyleSheet("QPushButton{"
                        "border:2px solid rgb(169,171,181);"
                        "background: transparent;"
                        "border-radius: 4px;"
                        "color:rgb(0,0,0);}"
                        "QPushButton:hover{"
                        "background-color: rgb(169,171,181);"
                        "color:white;"
                        "border:2px solid rgb(255,255,255);}");
}

changePasswordWindow::~changePasswordWindow()
{
    delete ui;
}

void changePasswordWindow::keyPressEvent(QKeyEvent *e)
{
    switch(e->key())
    {
    case Qt::Key_Up :
    {

        if(ui->oldPasswordEdit->hasFocus()||ui->newPasswordEdit->hasFocus())
        {
            ui->oldPasswordEdit->setFocus();
        }
        else if(ui->setPasswordEdit->hasFocus())
        {
            ui->newPasswordEdit->setFocus();
        }
        break;
    }
    case Qt::Key_Down :
    {
        if(ui->oldPasswordEdit->hasFocus())
        {
            ui->newPasswordEdit->setFocus();
        }
        else if(ui->newPasswordEdit->hasFocus()||ui->setPasswordEdit->hasFocus())
        {
            ui->setPasswordEdit->setFocus();
        }
        break;
    }
    }
}

void changePasswordWindow::change()
{
    QSqlQuery query1,query2;
    QString sql;
    QString str1 ,str2, str3;
    str1 = ui->oldPasswordEdit->text();
    str2 = ui->newPasswordEdit->text();
    str3 = ui->setPasswordEdit->text();
    if(str1==""||str2=="")
    {
        QMessageBox message(QMessageBox::NoIcon, "error", "旧密码或新密码为空！");
        message.exec();
        ui->oldPasswordEdit->clear();
        ui->newPasswordEdit->clear();
        ui->setPasswordEdit->clear();
        return ;
    }
    sql = QString("select * from users where name = '%1'").arg(username);
    query1.exec(sql);
    while(query1.next())
    {
        if(str1!=query1.value(2))
        {
            QMessageBox message(QMessageBox::NoIcon, "error", "旧密码输入错误");
            message.exec();
            ui->oldPasswordEdit->clear();
        }
        else if (str2 != str3)
        {
            QMessageBox message(QMessageBox::NoIcon, "error", "两次密码不一致");
            message.exec();
            ui->newPasswordEdit->clear();
            ui->setPasswordEdit->clear();
            return ;
        }
        else if (str2 == str3)
        {
            sql = QString("update users set password = '%1' where name = '%2'").arg(str2).arg(username);
            qDebug()<<sql;
            query2.exec(sql);
            QMessageBox message(QMessageBox::NoIcon, " ", "修改成功");
            message.exec();
            qDebug()<<"修改成功";
            ui->oldPasswordEdit->clear();
            ui->newPasswordEdit->clear();
            ui->setPasswordEdit->clear();
            this->close();
            return ;
        }
    }
}

void changePasswordWindow::setUser(QString user)
{
    username=user;
}

void changePasswordWindow::on_pushButton_2_clicked()
{
    this->close();
}

void changePasswordWindow::on_pushButton_clicked()
{
    change();
}
